package com.supersit.greensproject.Amap;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.LocationSource;

/**
 * Created by cong on 2016/7/15.
 */
public class AMap_LocationUtils {
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明mLocationOption对象
    public AMapLocationClientOption mLocationOption = null;
    private Context context;
    private GetResult getResult;

    public AMap_LocationUtils(Context context, GetResult getResult) {
        this.context = context;
        mLocationClient = new AMapLocationClient(context);
        this.getResult = getResult;
        configOption();
        setListener();
    }


    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            getResult.onLocationChanged(aMapLocation);
        }
    };

    private void configOption() {
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(true);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
    }

    private void setListener() {
        mLocationClient.setLocationListener(mLocationListener);
    }

    /**
     * 开始定位
     */
    public void startLocation() {
        mLocationClient.startLocation();
    }

    /**
     * 停止定位
     */
    public void stopLocation() {
        mLocationClient.stopLocation();
    }

    /**
     * 取消监听函数
     */
    public void unRegisterLocationListener() {
        mLocationClient.unRegisterLocationListener(mLocationListener);
    }

    public interface GetResult {
        public void onLocationChanged(AMapLocation aMapLocation);
    }
}
